package com.example.service.impl;

import com.example.annotation.Log;
import com.example.dto.CategoryDTO;
import com.example.mapper.CategoryMapper;
import com.example.result.PageBeanResult;
import com.example.service.CategoryService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryImpl  implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 展示所有分类
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    @Log
    public PageBeanResult showCategory(Integer page, Integer pageSize) {

        PageHelper.startPage(page,pageSize);
        Page<CategoryDTO> p= categoryMapper.showCategory(page,pageSize);
        Long total=p.getTotal();
        List<CategoryDTO> categoryDTOList=p.getResult();
        return new PageBeanResult(total,categoryDTOList);
    }

    /**
     * 按分类名字查询
     * @param name
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    @Log
    public PageBeanResult selectNameCategory(String name, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        Page<CategoryDTO> p=categoryMapper.selectNameCategory(name,page,pageSize);
        long total = p.getTotal();
        List<CategoryDTO> result = p.getResult();
        return new PageBeanResult(total,result);
    }


    /**
     * 启用禁用分类
     * @param status
     * @param id
     */
    @Override
    @Log
    public void starOrStop(Integer status, Integer id) {
        categoryMapper.starOrStop(status,id);

    }

    /**
     * 根据ID 删除分类
     * @param id
     */
    @Override
    public void deleteCategory(Integer id) {
        categoryMapper.deleteCategory(id);
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void deleteCategories(List<Integer> ids) {
        categoryMapper.deleteCategories(ids);
    }

    /**
     * 新增分类
     * @param categoryDTO
     */
    @Override
    @Log
    public void addCategory(CategoryDTO categoryDTO) {
        //categoryDTO.setCreateTime(LocalDateTime.now());
        //categoryDTO.setUpdateTime(LocalDateTime.now());
        categoryMapper.addCategory(categoryDTO);
    }

    /**
     * 数据回显
     * @param id
     * @return
     */
    @Override
    @Log
    public CategoryDTO selectCategory(Integer id) {
        return categoryMapper.selectCategory(id);
    }

    /**
     * 修改分类
     * @param categoryDTO
     */
    @Override
    @Log
    public void updateCategory(CategoryDTO categoryDTO) {
        //categoryDTO.setUpdateTime(LocalDateTime.now());
        categoryMapper.updateCategory(categoryDTO);
    }

    /**
     * 查询分类
     * @return
     */
    @Override
    public List<CategoryDTO> list() {
        return  categoryMapper.list();
    }
}
